package org.luxor.common.netty.protocol;

import io.netty.buffer.ByteBuf;
import org.luxor.common.netty.protocol.message.*;

/**
 * Utility class with factory methods to create different types of NETTY messages.
 *
 * @author Mr.yan @date 2024/2/22
 */
public class NettyMessageFactory {

    public static NettyMessage newMessage(NettyFixedHeader nettyFixedHeader, Object variableHeader, ByteBuf payload) {
        switch (nettyFixedHeader.messageType()) {
            case CONNECT:
                return new NettyConnectMessage(nettyFixedHeader, variableHeader);
            case CONN_ACK:
                return new NettyConnAckMessage(nettyFixedHeader, variableHeader);
            case PUBLISH:
                return new NettyPublishMessage(nettyFixedHeader, variableHeader, payload);
            case PUB_ACK:
                return new NettyPubAckMessage(nettyFixedHeader, variableHeader, payload);
            case PUB_REC:
                return new NettyPubRecMessage(nettyFixedHeader, variableHeader, payload);
            case PUB_REL:
                return new NettyPubRelMessage(nettyFixedHeader, variableHeader, payload);
            case PUB_COMP:
                return new NettyPubCompMessage(nettyFixedHeader, variableHeader, payload);
            case PING:
                return new NettyPingMessage(nettyFixedHeader);
            case PONG:
                return new NettyPongMessage(nettyFixedHeader);
            case DISCONNECT:
                return new NettyDisconnectMessage(nettyFixedHeader, variableHeader);
            default:
                throw new IllegalArgumentException("unknown message type: " + nettyFixedHeader.messageType());
        }
    }

}
